import 'package:edu/constants/app_strings.dart';
import 'package:edu/pages/profile/security/forgot_password_setting/forgot_password_setting_page_logic.dart';
import 'package:edu/pages/signin/verify_code_submit/verify_code_submit_page_logic.dart';
import 'package:edu/utils/app_router.dart';
import 'package:edu/utils/app_theme.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

class ForgotPasswordSettingPage extends StatefulWidget {
  const ForgotPasswordSettingPage({super.key});

  @override
  State<ForgotPasswordSettingPage> createState() =>
      _ForgotPasswordSettingPageState();
}

class _ForgotPasswordSettingPageState extends State<ForgotPasswordSettingPage> {
  @override
  void initState() {
    super.initState();
    context.read<ForgotPasswordSettingPageLogic>().init();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(context),
      body: _buildMainBody(context),
    );
  }

  AppBar _buildAppBar(BuildContext context) {
    return AppBar(title: const Text(AppStrings.findPassword));
  }

  Widget _buildMainBody(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20),
      child: Column(
        children: [
          const SizedBox(height: 20),
          _buildAccountInputRow(context),
          const SizedBox(height: 20),
          _buildMainButtonRow(context),
        ],
      ),
    );
  }

  Widget _buildAccountInputRow(BuildContext context) {
    final logic = context.watch<ForgotPasswordSettingPageLogic>();
    return Row(
      children: [
        Expanded(
          child: TextField(
            keyboardType: TextInputType.text,
            textInputAction: TextInputAction.done,
            controller: logic.textController,
            focusNode: logic.focusNode,
            autofocus: true,
            decoration: InputDecoration(
              contentPadding: const EdgeInsets.symmetric(
                horizontal: 12,
                vertical: 12,
              ),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(6),
                borderSide: BorderSide.none,
              ),
              fillColor: AppTheme.shared.inputTextFieldBackgroundColor,
              filled: true,
              hintText: AppStrings.accountHintText,
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildMainButtonRow(BuildContext context) {
    return SizedBox(
      height: 44,
      width: double.infinity,
      child: Selector<ForgotPasswordSettingPageLogic, bool>(
        selector: (context, logic) => logic.canSubmit,
        builder: (context, v, child) {
          return ElevatedButton(
            onPressed: v
                ? () {
                    _mainButtonPressed(context);
                  }
                : null,
            style: ButtonStyle(
              shape: WidgetStateProperty.all<RoundedRectangleBorder>(
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
              ),
            ),
            child: Text(
              AppStrings.submit,
              style: Theme.of(context).textTheme.titleMedium,
            ),
          );
        },
      ),
    );
  }

  // 主按钮点击
  void _mainButtonPressed(BuildContext context) {
    final logic = context.read<ForgotPasswordSettingPageLogic>();
    logic.submit().then((value) {
      if (value) {
        // 点击忘记密码，输入邮箱或手机号，跳转到密码和验证码提交页面，
        // 修改成功后，显示登录界面
        context.pushNamed(
          AppRouter.kVerifyCodeSubmit,
          queryParameters: {
            'submit_type': VerifyCodeSubmitPageLogic.kResetPassword,
            'hold_value': logic.textController.text,
            'prev_route': logic.prevRoute,
          },
        );
      }
    });
  }
}
